package com.example.pingze.controller;

import com.example.pingze.entity.PoemInfo;
import com.example.pingze.entity.PoetInfo;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.PoemInfoService;
import com.example.pingze.service.PoetInfoService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索页
 *
 * 获取name字段的首元素
 * 将首元素与百家姓中的每一个元素进行对比
 * 如果一致，则通过name获取诗人的相关信息
 * 如果不一致，则通过name获取诗词的相关信息
 */
@RestController
@RequestMapping("/records")
@CrossOrigin(origins = "*")
public class RecordsController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PoemInfoService poemInfoService;

    @Autowired
    private PoetInfoService poetInfoService;

    String[] Surname= {
            "赵","钱","孙","李","周","吴","郑","王","冯","陈","蒋","沈","韩","杨","朱","秦",
            "何","吕","张","孔","曹","魏","陶","姜","苏","范","彭","马","苗","袁","柳","鲍",
            "史","唐","岑","薛","雷","贺","汤","殷","罗","郝","安","于","齐","康","伍","元",
            "顾","孟","平","黄","穆","萧","尹","祁","毛","伏","戴","宋","屈","项","董","梁",
            "杜","席","季","麻","强","贾","路","危","江","童","颜","郭","林","徐","邱","骆",
            "高","夏","蔡","田","樊","胡","凌","霍","虞","卢","应","丁","邓","郁","洪","诸",
            "石","崔","龚","程","嵇","邢","裴","陆","翁","荀","于","惠","甄","曲","封","靳",
            "邴","松","段","富","巫","乌","焦","牧","隗","谷","刘","景","詹","叶","司","白",
            "乔","郁","翟","谭","晏","柴","瞿","廖","欧","蔚","越","巩","辛","阚","那","简",
            "空","曾","鞠","关","游","权","盖","益","岳","帅","楚","闫","高"};

    @PostMapping("/getPoemAndPoet")
    public Result<List> getPoemAndPoet(@RequestParam String name,@RequestParam String account){

        List<PoemInfo> poemInfoList = new ArrayList<>();
        List<PoetInfo> poetInfoList = new ArrayList<>();
        if (name == null) {
            throw new PingZeException("字段值为空");
        }
        if (account == null) {
            throw new PingZeException("用户账号为空");
        }
        insertValueInRedis(name,account);
        String subName = name.substring(0, 1);
        for (String s: Surname) {
            if (s.equals(subName)) {
                poetInfoList = poetInfoService.findPoetByName(name);
                Result result1 = new Result();
                result1.setCode("300");
                result1.setMsg("诗人信息");
                result1.setData(poetInfoList);
                return result1;
            }
        }
        if(poetInfoList.size() == 0) {
            poemInfoList = poemInfoService.findPoemByTitle(name);
            Result result2 = new Result();
            result2.setCode("301");
            result2.setMsg("诗词信息");
            result2.setData(poemInfoList);
            return result2;
        }
        return ResultVOUtils.success("没有您要的相关信息");
    }

    public void insertValueInRedis(String name, String account) {
        String key = "history:" + account;
        long top = 10;

        Double score = redisTemplate.opsForZSet().score(key, name);
        if(null != score){
            //表示该账号的该条历史记录已经存在
            redisTemplate.opsForZSet().remove(key, name);
        }
        //将数据存入redis
        redisTemplate.opsForZSet().add(key, name, System.currentTimeMillis());
        //获取key值对应的value的个数
        Long along = redisTemplate.opsForZSet().zCard(key);
        if(along > top){
            //只保留20条历史记录，超过20条数据的从redis中移除
            redisTemplate.opsForZSet().removeRange(key, 0, along - top - 1);
        }
    }
}
